<?php session_start();

if(isset($_SESSION['authorised']) && $_SESSION['authorised'] == true)
{
	if(isset($_SESSION['type']))
	{
		if($_SESSION['type'] == 'administrator')
		{
			include('connection.inc');
			$username=$_SESSION['manage_username'];
			$sql = "SELECT * FROM users WHERE username = '$username';";
			$result = pg_query($sql);
			$usertype = pg_fetch_result($result, 0, 'usertype');
			?>
			
			<head>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<title>Profile</title>
			<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
			<style>
			.nav {
				width: 210px;
				height: auto;
				margin-left: 10px;
				text-align: center;
			}
			
			.mainline {
				width: 712px;
				height: auto;
				text-align: center;
			}
			</style>
			</head>

<body bgcolor="#FFFFFF">
	<center>
		<div id="wrapper">
			<div id="header">
				<h1></h1>
			</div>

			<div id="header2">
				<h3></h3>
			</div>

			<div id="nav">
				<?php include 'admin_navigation.php';?>
			</div>

			<div id="mainline">
				<h4>View Profile</h4>
			</div>

			<div id="body">
				<center>
					<table width="600">
					
						<tr bgcolor="e8e8e8">
							<td width="200"><b>First Name:</b></td>
							<td><?php echo pg_fetch_result($result, 0, 'firstname');?></td>
						</tr>

						<tr bgcolor="#f5f5f5">
							<td><b>Middle Name:</b></td>
							<td><?php echo pg_fetch_result($result, 0, 'middlename');?></td>
						</tr>

						<tr bgcolor="e8e8e8">
							<td><b>Last Name:</b></td>
							<td><?php echo pg_fetch_result($result, 0, 'lastname');?></td>
						</tr>

						<tr bgcolor="#f5f5f5">
							<td><b>Email:</b></td>
							<td>
								<?php 
									$username=pg_fetch_result($result, 0, 'username');
									echo $username;
								?>
							</td>
						</tr>
						
						<tr bgcolor="e8e8e8">
							<td><b>Contact Number:</b></td>
							<td><?php echo pg_fetch_result($result, 0, 'contactnum')?></td>
						</tr>

						<tr bgcolor="#f5f5f5"> 
							<td><b>Street Address 1:</b></td>
							<td><?php echo pg_fetch_result($result, 0, 'streetadd1');?></td>
						</tr>

						<tr bgcolor="e8e8e8">
							<td><b>Street Address 2:</b></td>
							<td><?php echo pg_fetch_result($result, 0, 'streetadd2');?></td>
						</tr>

						<tr bgcolor="#f5f5f5">
							<td><b>City:</b></td>
							<td><?php echo pg_fetch_result($result, 0, 'cityadd');?></td>
						</tr>
						
						<?php 
						
						if($usertype=='parent')
						{
							$query = "select * from parents where username = '$username'";
							$result = pg_query($query);
							$parent_id = pg_fetch_result($result, 0, 'parent_id');
							$sql = "select * from students where parent_id = '$parent_id'";
							$result = pg_query($sql);
							$count = pg_num_rows($result);
							for($i = 0; $i < $count; $i++)
	            			{
								$student = pg_fetch_result($result, $i, 'username');
								$sql = "select * from users where username = '$student'";
								$result = pg_query($sql);
								$stud_last=pg_fetch_result($result, 0, 'lastname');
								$stud_first=pg_fetch_result($result, 0, 'firstname');
								$stud_middle=pg_fetch_result($result, 0, 'middlename');
								echo "<tr bgcolor=\"#e8e8e8\">
								<td><b>Child:</b></td>
								<td>$stud_last, $stud_first $stud_middle</td>
								</tr>";
	            			}
						}
						
						elseif($usertype=='student')
						{
							$query1 = "select * from students where username = '$username'";
							$result1 = pg_query($query1);
							$parent_id = pg_fetch_result($result1, 0, 'parent_id');
							$query = "select * from parents where parent_id = '$parent_id'";
							$result = pg_query($query);
							$parent = pg_fetch_result($result, 0, 'username');
							$sql = "select * from users where username = '$parent'";
							$result = pg_query($sql);
							$parent_last=pg_fetch_result($result, 0, 'lastname');
							$parent_first=pg_fetch_result($result, 0, 'firstname');
							$parent_middle=pg_fetch_result($result, 0, 'middlename');
							?>
						<tr bgcolor="e8e8e8">
							<td><b>Parent:</b></td>
							<td><?php echo "$parent_last, $parent_first $parent_middle";?></td>
						</tr>
						<tr bgcolor="#f5f5f5">
							<td><b>Grade Level:</b></td>
							<td><?php echo pg_fetch_result($result1, 0, 'grade_level')?></td>
						</tr>

						<tr bgcolor="e8e8e8">
							<td><b>School:</b></td>
							<td><?php echo pg_fetch_result($result1, 0, 'school')?></td>
						</tr>

					<?php 	}
					elseif($usertype=='tutor')
						{
							$query = "select * from teachers where username = '$username'";
							$result = pg_query($query);
							
							$emergency_contact_person=pg_fetch_result($result, 0, 'emergency_contact_person');
							$emergency_contact_number=pg_fetch_result($result, 0, 'emergency_contact_number');
							$educational_attainment=pg_fetch_result($result, 0, 'educational_attainment');
							echo "<tr bgcolor=\"#e8e8e8\">
							<td><b>Educational Attainment:</b></td>
							<td>$educational_attainment</td>
							</tr>
							<tr bgcolor=\"#f5f5f5\">
							<td><b>Emergency Contact Person:</b></td>
							<td>$emergency_contact_person</td>
							</tr>
							<tr bgcolor=\"#e8e8e8\">
							<td><b>Emergency Contact Number</b></td>
							<td>$emergency_contact_number</td>
							</tr>";
							
            			}

						
						?>
					</table><br><br>
					<form action=admin_manageaccounts.php method=POST>
  		<input type=submit name="back" value="Back"  />
  		</form>
				</center>
			</div>

			<p align="justify">
				<br />
			</p>
		

		<br class="clearfloat"> <br />

		<div id="footer">
			<p align="center">
				Tutorial Business Manager: info@tbms.com <br />Credits/Engineers:
				Aquino, Chloebelle; Dela Cruz, Roy Erwin; Maliwanag, Ranyel Bryan;
				Manzano, Audrey Ann; Ng, Sheila Regine<br /> <br />copyright 2011
			</p>
		</div>
		</div>
	</center>
</body>
			<?php 
		}
		elseif($_SESSION['type'] == 'student')
		{
			header('Location: student_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'tutor')
		{
			header('Location: tutor_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'parent')
		{
			
			header('Location: parent_index.php');
			exit();
		}
	}
}
else
{
	header('Location: index.php');
}
?>
